//
//  HOReportLocationViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/15/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOReportLocationViewController.h"
#import "HOStaticDataMgr.h"
#import "HOConstants.h"
#import "HOAreaEntity.h"
#import "HOUIMgr.h"
#import "HOGlobalData.h"
#import "HOUtils.h"

@interface HOReportLocationViewController ()
{
	UITableView *secondTableView;
	UITableView *thirdTableView;
}
@property(nonatomic, retain)NSMutableArray *firstAreaList;
@property(nonatomic, retain)NSMutableArray *secondAreaList;
@property(nonatomic, retain)NSMutableArray *thirdAreaList;

@end

@implementation HOReportLocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initDataSource];
	[self initTableView];
	[self initSubviews];
	[self requestData];
}

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	if(self.isSelectedTheFirstArea)
	{
		[self setNavigationItemTitle:NSLocalizedString(@"选择过滤报修地点", @"选择过滤报修地点")];
	}
	else
	{
		[self setNavigationItemTitle:NSLocalizedString(@"选择报修地点", @"选择报修地点")];
	}
	CGFloat width = self.view.frame.size.width /[HOGlobalData sharedInstance].levelType - 5;
	
	baseTableview.frame = CGRectMake(0,
									 baseTableview.frame.origin.y,
									 width,
									 [HOUtils getScreenSize].height - self.navigationController.navigationBar.frame.size.height - [HOUtils getStatusBarHeight]);
	secondTableView.frame = CGRectMake(width,
									   baseTableview.frame.origin.y,
									   width,
									   baseTableview.frame.size.height);
	thirdTableView.frame = CGRectMake(2 * width,
									  baseTableview.frame.origin.y,
									  width + 15,
									  baseTableview.frame.size.height);
	if(self.isSelectedTheFirstArea)
	{
		baseTableview.frame = CGRectMake(0,
										 baseTableview.frame.origin.y,
										 self.view.frame.size.width,
										 [HOUtils getScreenSize].height - self.navigationController.navigationBar.frame.size.height - [HOUtils getStatusBarHeight]);
	}
	if(self.isSelectedTheFirstArea)
	{
		secondTableView.hidden = YES;
		thirdTableView.hidden = YES;
	}
	[self requestData];
}

- (void)initDataSource
{
	self.firstAreaList = [[[NSMutableArray alloc] init] autorelease];
	self.secondAreaList = [[[NSMutableArray alloc] init] autorelease];
	self.thirdAreaList = [[[NSMutableArray alloc] init] autorelease];
}

- (void)initSubviews
{
	CGFloat width = self.view.frame.size.width /[HOGlobalData sharedInstance].levelType;
	baseTableview.frame = CGRectMake(0,
									 baseTableview.frame.origin.y,
									 width,
									 self.view.frame.size.height);
	secondTableView = [[self createTableViewWithFrame:CGRectMake(self.view.frame.size.width / 2,
																 baseTableview.frame.origin.y,
																 width,
																 self.view.frame.size.height)] retain] ;
	[self.view addSubview:secondTableView];
	
	thirdTableView = [[self createTableViewWithFrame:CGRectMake(self.view.frame.size.width / 2,
																baseTableview.frame.origin.y,
																width,
																self.view.frame.size.height)] retain];
	[self.view addSubview:thirdTableView];
	secondTableView.hidden = [HOGlobalData sharedInstance].levelType < HO_AERA_LEVEL2;
	secondTableView.hidden = [HOGlobalData sharedInstance].levelType < HO_AERA_LEVEL3;
	
	if([HOGlobalData sharedInstance].selType == HO_AREA_SEL_MULTI)
	{
		[self displayRightButtomItemWithTitle:NSLocalizedString(@"选定", @"选定")];
	}
	else
	{
		[self displayRightButtomItemWithTitle:@""];
	}
		
}

- (void)dealloc
{
	self.selectedFirstAreaEntity = nil;
	self.selectedSecondAreaEntity = nil;
	self.selectedThirdAreaEntity = nil;
	
	self.firstAreaList = nil;
	self.secondAreaList = nil;
	self.thirdAreaList = nil;
	
	self.delegate = nil;
	
	[secondTableView release];
	secondTableView.delegate = nil;
	secondTableView.dataSource = nil;
	
	[thirdTableView release];
	thirdTableView.delegate = nil;
	thirdTableView.dataSource = nil;
	
	[super dealloc];
}


- (void)rightButtonItemDidClick
{
	[self.delegate didSelectAreaEntity:self.selectedFirstAreaEntity secondEntity:self.selectedSecondAreaEntity withThirdArea:self.selectedThirdAreaEntity];
	[self goBack];
}

- (UITableView *)createTableViewWithFrame:(CGRect)frame
{
	UITableView *tableView = [[[UITableView alloc]initWithFrame:frame
														   style:UITableViewStyleGrouped] autorelease];
	[tableView setBackgroundColor:MAIN_BG_COLOR];
	tableView.delaysContentTouches = NO;
	tableView.delegate = self;
	tableView.dataSource = self;
	tableView.sectionFooterHeight = 0;
	tableView.separatorColor = TEXT_GRAY_DARK_dbdbdb;
	
	tableView.frame = frame;
	return tableView;
}

- (void)requestData
{
	self.selectedFirstAreaEntity = nil;
	self.selectedSecondAreaEntity = nil;
	self.selectedThirdAreaEntity = nil;
	[self.firstAreaList removeAllObjects];
	[self.secondAreaList removeAllObjects];
	[self.thirdAreaList removeAllObjects];
	[thirdTableView reloadData];
	[secondTableView reloadData];
	[thirdTableView reloadData];
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetFaultLocationList}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return CELL_HEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(tableView == baseTableview)
	{
		NSInteger count = [self.firstAreaList count];
		if(count == 0)
		{
			return  0;
		}
		if(self.selectedFirstAreaEntity == nil)
		{
			self.selectedFirstAreaEntity = [self.firstAreaList firstObject];
		}
		return count;
	}
	else if(tableView == secondTableView)
	{
		self.secondAreaList = self.selectedFirstAreaEntity.subAreaList;
		if(self.secondAreaList == nil)
			return 0;
		
		if(self.selectedSecondAreaEntity == nil)
		{
			self.selectedSecondAreaEntity = [self.secondAreaList firstObject];
		}
		return [self.secondAreaList count];
	}
	else if(tableView == thirdTableView)
	{
		self.thirdAreaList = self.selectedSecondAreaEntity.subAreaList;
		if(self.thirdAreaList == nil)
			return 0;
		
		if(self.selectedThirdAreaEntity == nil)
		{
			self.selectedThirdAreaEntity = [self.thirdAreaList firstObject];
		}
		return [self.thirdAreaList count];
	}
	
	return 0;
}

- (HOAreaEntity *)tableView:(UITableView *)tableView areaForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(tableView == baseTableview)
	{
		return [self.firstAreaList objectAtIndex:indexPath.row];
	}
	else if(tableView == secondTableView)
	{
		if(!self.secondAreaList)
			return nil;
		return [self.secondAreaList objectAtIndex:indexPath.row];
	}
	else
	{
		if(!self.thirdAreaList)
			return nil;
		return [self.thirdAreaList objectAtIndex:indexPath.row];
	}
	return nil;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	
	UILabel *lbl =(UILabel *)[cell viewWithTag:112];
	if(!lbl)
	{
		lbl = [[[UILabel alloc] initWithFrame:CGRectMake(1,
														 (CELL_HEIGHT - 22) / 2,
														 tableView.frame.size.width - 24,
														 22)] autorelease];
		lbl.tag = 112;
		lbl.numberOfLines = 2;
		lbl.font = [UIFont systemFontOfSize:self.isSelectedTheFirstArea ? 15 : 13];
		lbl.textColor = TEXT_COLOR_181818;
		[cell.contentView addSubview:lbl];
	}
	
	UIImageView *imgView =(UIImageView *)[cell viewWithTag:113];
	if(!imgView)
	{
		imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 12,
														 (CELL_HEIGHT - 16) / 2,
														 9,
														 16)] autorelease];
		imgView.image = [UIImage imageNamed:@"ic_taizhuanyemian"];
		imgView.tag = 113;

		[cell.contentView addSubview:imgView];
	}
//	imgView.hidden = (tableView != thirdTableView || [HOGlobalData sharedInstance].selType == HO_AREA_SEL_MULTI);
	HOAreaEntity *entity = [self tableView:tableView areaForRowAtIndexPath:indexPath];
	if(!entity)
		return nil;
//	if([entity.subAreaList count] == 0)
//	{
//		imgView.hidden = YES;
//	}
	lbl.text = entity.areaName;
	[lbl sizeToFit];
	lbl.frame = CGRectMake(5,
						  (CELL_HEIGHT - lbl.frame.size.height) / 2,
						  tableView.frame.size.width - 10,
						   lbl.frame.size.height);
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(tableView == baseTableview)
	{
		HOAreaEntity *newArea = [self.firstAreaList objectAtIndex:indexPath.row];
		if(self.selectedFirstAreaEntity.areaId == newArea.areaId)
		{
			if([self.selectedFirstAreaEntity.subAreaList count] == 0)
			{
				[self rightButtonItemDidClick];
			}
			return;
		}
		self.selectedFirstAreaEntity = newArea;
		if(self.isSelectedTheFirstArea)
		{
			[self rightButtonItemDidClick];
			return;
		}
		
		self.selectedSecondAreaEntity = [self selectNextAreaFrom:self.selectedFirstAreaEntity withTableView:secondTableView];
		self.selectedThirdAreaEntity = [self selectNextAreaFrom:self.selectedSecondAreaEntity withTableView:thirdTableView];
		[secondTableView reloadData];
		[thirdTableView reloadData];
		if([self.selectedFirstAreaEntity.subAreaList count] == 0)
		{
			if([HOGlobalData sharedInstance].selType == HO_AREA_SEL_FULL)
			{
				[self rightButtonItemDidClick];
			}
		}

	}
	else if(tableView == secondTableView)
	{
	 	HOAreaEntity *newArea = [self.secondAreaList objectAtIndex:indexPath.row];
		if(self.selectedSecondAreaEntity.areaId == newArea.areaId)
		{
			if([self.selectedSecondAreaEntity.subAreaList count] == 0)
			{
				[self rightButtonItemDidClick];
			}
			return;
		}
		self.selectedSecondAreaEntity = newArea;
		self.selectedThirdAreaEntity = [self selectNextAreaFrom:self.selectedSecondAreaEntity withTableView:thirdTableView];
		
		[thirdTableView reloadData];
		if([self.selectedSecondAreaEntity.subAreaList count] == 0)
		{
			if([HOGlobalData sharedInstance].selType == HO_AREA_SEL_FULL)
			{
				[self rightButtonItemDidClick];
			}
		}
	}
	else
	{
		HOAreaEntity *newArea = [self.thirdAreaList objectAtIndex:indexPath.row];
		if(self.selectedThirdAreaEntity.areaId == newArea.areaId)
		{
			if([self.selectedThirdAreaEntity.subAreaList count] == 0)
			{
				[self rightButtonItemDidClick];
			}
			return;
		}
		self.selectedThirdAreaEntity = newArea;
		if([HOGlobalData sharedInstance].selType == HO_AREA_SEL_FULL)
		{
			[self rightButtonItemDidClick];
		}
	}
	
}

- (HOAreaEntity *)selectNextAreaFrom:(HOAreaEntity *)areaEntity withTableView:(UITableView *)tableView
{
	if(areaEntity == nil || [areaEntity.subAreaList count] == 0)
		return nil;
	HOAreaEntity *nextAreaEntity = [areaEntity.subAreaList firstObject];
	return nextAreaEntity;
}

#pragma mark
- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetFaultLocationList])
	{
		for(int i = 0; i < [response.dataNode count]; i ++)
		{
			NSDictionary *dic = [response.dataNode objectAtIndex:i];
			int areaId = [[dic objectForKey:kHO_FIELD_AreaId] intValue];
			HOAreaEntity *areaEntity = [[HOStaticDataMgr sharedInstance] getAreaEntityWithId:areaId];
			if(![self.firstAreaList containsObject:areaEntity])
			{
				[self.firstAreaList addObject:areaEntity];
			}
			[areaEntity parseDic:dic];
			areaEntity.index = i;
		}
		[baseTableview reloadData];
		[self selectTheDefault];
	}
}

- (void)selectTheDefault
{
	if([self.firstAreaList count] == 0)
		return;
	if(self.selectedFirstAreaEntity == nil)
		self.selectedFirstAreaEntity = [self.firstAreaList firstObject];
	if(self.selectedFirstAreaEntity.index < [self.firstAreaList count])
	{
		[baseTableview selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedFirstAreaEntity.index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
		[baseTableview scrollsToTop];
	}
	self.secondAreaList = self.selectedFirstAreaEntity.subAreaList;
	if([self.secondAreaList count] == 0)
		return;
	[secondTableView reloadData];
	
	if(self.selectedSecondAreaEntity == nil)
		self.selectedSecondAreaEntity = [self.secondAreaList firstObject];
	if(self.selectedSecondAreaEntity.index < [self.secondAreaList count])
	{
		[secondTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedSecondAreaEntity.index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
		[secondTableView scrollsToTop];
	}
	
	
	self.thirdAreaList = self.selectedSecondAreaEntity.subAreaList;
	if([self.thirdAreaList count] == 0)
		return;
	
	[thirdTableView reloadData];
	
	if(self.selectedThirdAreaEntity == nil)
		self.selectedThirdAreaEntity = [self.thirdAreaList firstObject];
	if(self.selectedThirdAreaEntity.index < [self.thirdAreaList count])
	{
		[thirdTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedThirdAreaEntity.index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
		[thirdTableView scrollsToTop];
	}
	
}

@end
